<template>
  <a-popover
    trigger="click"
    placement="bottomRight"
    :autoAdjustOverflow="true"
    :arrowPointAtCenter="true"
    overlayClassName="header-notice-wrapper"
    @visibleChange="handleHoverChange"
    :overlayStyle="{ width: '450px', top: '50px' }">
    <template slot="content">
      <a-spin :spinning="loadding">
        <a-tabs>
          <a-tab-pane :tab="msg1Title" key="1">
            <a-list :dataSource="listHolder">
              <a-list-item slot="renderItem" slot-scope="item, index">
                <a slot="actions"><router-link to="/workflow/task">查看</router-link></a>
                <a-list-item-meta>
                  <a slot="title">待审批任务</a>
                </a-list-item-meta>
                <div>{{msg1Count}}</div>
              </a-list-item>
            </a-list>
          </a-tab-pane>
          <a-tab-pane tab="待出库设备/工器具" key="3">
            <a-list :dataSource="listHolder2">
              <a-list-item slot="renderItem" slot-scope="item, index">
                <a slot="actions"><router-link :to="item.to">查看</router-link></a>
                <a-list-item-meta>
                  <a slot="title">{{item.title}}</a>
                </a-list-item-meta>
                <div>{{item.total}}</div>
              </a-list-item>
            </a-list>
          </a-tab-pane>
        </a-tabs>
      </a-spin>
    </template>
    <span @click="fetchNotice" class="header-notice">
      <a-badge :count="msgTotal">
        <a-icon style="font-size: 16px; padding: 4px" type="bell" />
      </a-badge>
    </span>
    <show-announcement ref="ShowAnnouncement" @ok="modalFormOk"></show-announcement>
  </a-popover>
</template>

<script>
  import { getAction,putAction } from '@/api/manage'
  import ShowAnnouncement from './ShowAnnouncement'

  export default {
    name: "HeaderNotice",
    components: {
      ShowAnnouncement,
    },
    data () {
      return {
        loadding: false,
        listHolder:[{t:1}],
        listHolder2:[{to:"/asset/outapply",title:"待出库设备",total:0},{to:"/tools/outApply",title:"待出库工器具",total:0}],
        url:{
          listCementByUser:"/tasks/applyTask/count",
          editCementSend:"/system/sysAnnouncementSend/editByAnntIdAndUserId",
        },
        hovered: false,
        announcement1:[],
        announcement2:[],
        msg1Count:"0",
        msg2Count:"0",
        msg1Total:0,
        msg2Total:0,
        msg1Title:"通知(3)",
        msg2Title:"设备状况",
      }
    },
    computed:{
      msgTotal () {
        return parseInt(this.msg1Count)+parseInt(this.msg2Count)+this.listHolder2[0].total+this.listHolder2[1].total;
      }
    },
    created() {
      this.loadData();
      this.timer();
    },
    methods: {
      timer() {
        return setInterval(()=>{
          this.loadData()
        },60000)
      },
      loadData (){
        try {
           getAction("/assetoutapply/assetOutApply01/wcount").then((res) => {
            if (res.success) {
              this.listHolder2[0].total = res.result;
            }
          });
           getAction("/toolsoutapply/toolsOutApply01/wcount").then((res) => {
            if (res.success) {
              this.listHolder2[1].total = res.result;
            }
          });
          // 获取系统消息
          getAction(this.url.listCementByUser).then((res) => {
            if (res.success) {
              this.msg1Count = res.result;
              this.msg1Title = "待审批任务";
            }
          });
        } catch (err) {
        }
      },
      fetchNotice () {
        if (this.loadding) {
          this.loadding = false
          return
        }
        this.loadding = true
        setTimeout(() => {
          this.loadding = false
        }, 200)
      },
      showAnnouncement(record){
        putAction(this.url.editCementSend,{anntId:record.id}).then((res)=>{
          if(res.success){
            this.loadData();
          }
        });
        this.hovered = false;
        this.$refs.ShowAnnouncement.detail(record);
      },
      toMyAnnouncement(){

        this.$router.push({
          path: '/isps/userAnnouncement',
          name: 'isps-userAnnouncement'
        });
      },
      modalFormOk(){
      },
      handleHoverChange (visible) {
        this.hovered = visible;
      },

    }
  }
</script>

<style lang="css">
  .header-notice-wrapper {
    top: 50px !important;
  }
</style>
<style lang="scss" scoped>
  .header-notice{
    display: inline-block;
    transition: all 0.3s;

    span {
      vertical-align: initial;
    }
  }
</style>